package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.comon.CustomException;
import com.itheima.reggie.domain.Category;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper,Category> implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    //查询菜系和套餐
    @Override
    public List<Category> findAll() {
        return categoryMapper.findAll();
    }

    //添加菜系和套餐
    @Override
    public void addStyleOfCooking(Category category) {
    /*    //设置id
        category.setId(IdUtil.createSnowflake(1, 1).nextId());
        //设置添加时间和修改时间
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        //设置添加人和修改人
        category.setCreateUser(1L);
        category.setUpdateUser(1L);*/
        //添加到数据库
        categoryMapper.addStyleOfCooking(category);
    }

    //更新菜系和套餐
    @Override
    public void updateStyleOfCooking(Category category) {
     /*   //设置修改的时间
        category.setUpdateTime(new Date());
        //设置修改人
        category.setUpdateUser(1L);*/
        categoryMapper.updateStyleOfCooking(category);

    }

    //删除菜系和套餐
    @Override
    public void deleteStyleOfCooking(Long id) {
        //判断菜系或套餐里有没有具体的菜
        Integer categoryId = categoryMapper.countDishCategoryId(id);
        if (categoryId > 0) {
            throw new CustomException("菜系里还有菜没删呢,请删除后或将菜品移到别的菜系后重试~");
        }
        Integer setmealId = categoryMapper.countSetmealCategoryId(id);
        if (setmealId > 0) {
             throw new CustomException("套餐里还有菜没删呢,请删除后或将菜品移到别的套餐后重试~");
        }
        categoryMapper.deleteStyleOfCooking(id);
    }
}
